package hanlder

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-manager/cache"
	"order-manager/models"
	"order-manager/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "login_" + username
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "错误超过三次，禁止登陆",
		})
		return
	}
	var user models.User
	err := models.DB.Where("username = ?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "封禁两小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登陆失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
		"data": user,
	})
	return
}
func RoleNodeAdd(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}

}
func RoleList(c *gin.Context) {
	uid, _ := strconv.Atoi(c.Query("uid"))
	var user []models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN users ON users.id = roles.uid").
		Where("roles.uid = ?", uid).
		Find(&user)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": user,
	})
	return
}
func NodeList(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	var user []models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN roles ON roles.id = nodes.rid").
		Where("nodes.rid = ?", rid).
		Find(&user)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": user,
	})
	return
}
func ShangAdd(c *gin.Context) {
	shang_id := c.PostForm("shang_id")
	shang_xin := c.PostForm("shang_xin")
	shang_type := c.PostForm("shang_type")
	shang_price := c.PostForm("shang_price")

	data := models.Shang{
		ShangID:    shang_id,
		ShangXin:   shang_xin,
		ShangType:  shang_type,
		ShangPrice: shang_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}

}
func DingAdd(c *gin.Context) {
	ding_id := c.PostForm("ding_id")
	ding_name := c.PostForm("ding_name")
	ding_zhi := c.PostForm("ding_zhi")
	sid, _ := strconv.Atoi(c.PostForm("sid"))

	data := models.Ding{
		DingID:   ding_id,
		DingName: ding_name,
		DingZhi:  ding_zhi,
		Sid:      sid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}

}
func ShangDingList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	var user []models.ShangDingList
	offset := (page - 1) * size
	redis_key := "shang_ding_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("dings").
			Select(
				"dings.id AS DingId",
				"dings.ding_id AS DingID",
				"dings.ding_name AS DingName",
				"dings.ding_zhi AS DingZhi",
				"shangs.shang_id AS ShangID",
				"shangs.shang_xin AS ShangXin",
				"shangs.shang_type AS ShangType",
				"shangs.shang_price AS ShangPrice",
			).
			Joins("LEFT JOIN shangs ON shangs.id = dings.sid").
			Offset(offset).
			Limit(size).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	var total int64
	models.DB.Model(&models.Ding{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "列表展示成功",
		"data":  user,
		"total": total,
	})
	return

}
func ShangList(c *gin.Context) {
	id := c.Query("id")
	var user models.Ding
	redis_key := "shang_ding" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.
			Where("id = ?", id).
			Find(&user)
		jsonData, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &user)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表展示成功",
		"data": user,
	})
	return

}
